﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Markup;
using System.Xml;
using UBCon.Navigation.SkinManagers;

namespace UBCon.Navigation
{
    public static class NavigationHelper
    {
        internal static T XamlClone<T>(T source)
        {
            string savedObject = XamlWriter.Save(source);
            
            StringReader stringReader = new StringReader(savedObject);
            
            XmlReader xmlReader = XmlReader.Create(stringReader);
            
            T target = (T)XamlReader.Load(xmlReader);

            return target;
        }

        //------------------------------------------------------
        //
        //  Constructors
        //
        //------------------------------------------------------
        #region Constructors
        #endregion

        //------------------------------------------------------
        //
        //  Properties
        //
        //------------------------------------------------------
        #region Properties

        /// <summary>
        /// Gets the value of the UBCon.Navigation.NavigationHelper.EnableCustomStyle attached property for the 
        /// given dependency object.
        /// </summary>
        public static bool GetEnableCustomStyle(DependencyObject obj)
        {
            return (bool)obj.GetValue(EnableCustomStyleProperty);
        }

        /// <summary>
        /// Sets the value of the UBCon.Navigation.NavigationHelper.EnableCustomStyle attached property for the 
        /// given dependency object.
        /// </summary>
        public static void SetEnableCustomStyle(DependencyObject obj, bool value)
        {
            obj.SetValue(EnableCustomStyleProperty, value);
        }

        /// <summary>
        /// Identifies UBCon.Navigation.NavigationHelper.EnableCustomStyle attached property.
        /// </summary>
        public static readonly DependencyProperty EnableCustomStyleProperty =
            DependencyProperty.RegisterAttached("EnableCustomStyle", typeof(bool), typeof(NavigationHelper),
            new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnEnableCustomStylePropertyChanged)));

        /// <summary>
        /// Gets the value of the UBCon.Navigation.NavigationHelper.CornerRadius attached property for the 
        /// given dependency object.
        /// </summary>
        public static CornerRadius GetCornerRadius(DependencyObject obj)
        {
            return (CornerRadius)obj.GetValue(CornerRadiusProperty);
        }

        /// <summary>
        /// Sets the value of the UBCon.Navigation.NavigationHelper.CornerRadius attached property for the 
        /// given dependency object.
        /// </summary>
        public static void SetCornerRadius(DependencyObject obj, CornerRadius value)
        {
            obj.SetValue(CornerRadiusProperty, value);
        }

        /// <summary>
        /// Identifies UBCon.Navigation.NavigationHelper.CornerRadius attached property.
        /// </summary>
        public static readonly DependencyProperty CornerRadiusProperty =
            DependencyProperty.RegisterAttached("CornerRadius", typeof(CornerRadius), typeof(NavigationHelper), new PropertyMetadata(new CornerRadius(3.0)));

        #endregion

        //------------------------------------------------------
        //
        //  Depenency Property Changed Callbacks
        //
        //------------------------------------------------------
        #region Depenency Property Changed Callbacks

        /// <summary>
        /// Invoked whenever EnableCustomStyle attached property changes.
        /// </summary>
        /// <param name="sender">Type: System.Windows.DependencyObject. The DependencyObject on which the property has changed value.</param>
        /// <param name="e">Type: System.Windows.DependencyPropertyChangedEventArgs. Event data that is issued by any event that tracks changes to the effective value of this property.</param>
        private static void OnEnableCustomStylePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            if (sender is Button)
            {
                Button button = sender as Button;

                ComponentResourceKey rkey = new ComponentResourceKey(typeof(NavigationHelper), "CustomButtonStyle");

                Style style = GetThemeResourceByKey<Style>(rkey);

                if (style != null)
                {
                    button.SetValue(Button.StyleProperty, style);
                }
            }
            else if (sender is ToggleButton)
            {

            }
            else if (sender is RepeatButton)
            {
                RepeatButton button = sender as RepeatButton;

                ComponentResourceKey rkey = new ComponentResourceKey(typeof(NavigationHelper), "CustomRepeatButtonStyle");

                Style style = GetThemeResourceByKey<Style>(rkey);

                if (style != null)
                {
                    button.SetValue(RepeatButton.StyleProperty, style);
                }
            }
        }
        #endregion

        //------------------------------------------------------
        //
        //  Overrides
        //
        //------------------------------------------------------
        #region Overrides

        #endregion

        //------------------------------------------------------
        //
        //  Public Methods
        //
        //------------------------------------------------------
        #region Public Methods
        #endregion

        //------------------------------------------------------
        //
        //  Internal Methods
        //
        //------------------------------------------------------
        #region Internal Methods

        /// <summary>
        /// Get resource with specified key.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns>Specified type of object.</returns>
        internal static T GetThemeResourceByKey<T>(object key) where T : class
        {
            try
            {
                if (SkinManager.OfficeBlueTheme != null)
                {
                    return SkinManager.OfficeBlueTheme[key] as T;
                }

                return null;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// Get resource with specified key.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns>Specified type of object.</returns>
        internal static T GetSkinResourceByKey<T>(object key) where T : class
        {
            try
            {
                if (SkinManager.OfficeBlueSkin != null)
                {
                    return SkinManager.OfficeBlueSkin[key] as T;
                }

                return null;
            }
            catch
            {
                return null;
            }
        }

        #endregion

        //------------------------------------------------------
        //
        //  Private Methods
        //
        //------------------------------------------------------
        #region Private Methods
        #endregion

        //------------------------------------------------------
        //
        //  Event Handlers
        //
        //------------------------------------------------------
        #region Event Handlers
        #endregion

        //------------------------------------------------------
        //
        //  Fields
        //
        //------------------------------------------------------
        #region Fields
        #endregion

        //------------------------------------------------------
        //
        //  Events
        //
        //------------------------------------------------------
        #region Events
        #endregion

        //------------------------------------------------------
        //
        //  Commands
        //
        //------------------------------------------------------
        #region Commands
        #endregion

    }
}
